package pl.dmcs.grails

import static org.springframework.http.HttpStatus.*
import grails.plugin.springsecurity.annotation.Secured
import grails.transaction.Transactional

@Transactional(readOnly = true)
class EmployeeCategoryController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    @Secured(["ROLE_DMCS_ADMIN"])
    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond EmployeeCategory.list(params), model:[employeeCategoryInstanceCount: EmployeeCategory.count()]
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    def show(EmployeeCategory employeeCategoryInstance) {
        respond employeeCategoryInstance
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    def create() {
        respond new EmployeeCategory(params)
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    @Transactional
    def save(EmployeeCategory employeeCategoryInstance) {
        if (employeeCategoryInstance == null) {
            notFound()
            return
        }

        if (employeeCategoryInstance.hasErrors()) {
            respond employeeCategoryInstance.errors, view:'create'
            return
        }

        employeeCategoryInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'employeeCategory.label', default: 'EmployeeCategory'), employeeCategoryInstance.id])
                redirect employeeCategoryInstance
            }
            '*' { respond employeeCategoryInstance, [status: CREATED] }
        }
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    def edit(EmployeeCategory employeeCategoryInstance) {
        respond employeeCategoryInstance
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    @Transactional
    def update(EmployeeCategory employeeCategoryInstance) {
        if (employeeCategoryInstance == null) {
            notFound()
            return
        }

        if (employeeCategoryInstance.hasErrors()) {
            respond employeeCategoryInstance.errors, view:'edit'
            return
        }

        employeeCategoryInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'EmployeeCategory.label', default: 'EmployeeCategory'), employeeCategoryInstance.id])
                redirect employeeCategoryInstance
            }
            '*'{ respond employeeCategoryInstance, [status: OK] }
        }
    }

    @Secured(["ROLE_DMCS_ADMIN"])
    @Transactional
    def delete(EmployeeCategory employeeCategoryInstance) {

        if (employeeCategoryInstance == null) {
            notFound()
            return
        }

        employeeCategoryInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'EmployeeCategory.label', default: 'EmployeeCategory'), employeeCategoryInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'employeeCategory.label', default: 'EmployeeCategory'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
